package com.shangqing.system.duststorminfomation.service.Impl;

import com.shangqing.system.droughtinformation.entity.City;
import com.shangqing.system.duststorminfomation.entity.DustStorm;
import com.shangqing.system.duststorminfomation.mapper.DustStormMapper;
import com.shangqing.system.duststorminfomation.service.IdustStormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class DustStormImpl implements IdustStormService {
    @Autowired
    private DustStormMapper dustStormMapper;
    @Override
    public List<DustStorm> getAll() {
        List<DustStorm> dustStormList = new ArrayList<>();
        List<DustStorm> dustStorms = dustStormMapper.getAllInfo();
        City city=null;
        for (DustStorm d : dustStorms
        ) {
            d.setDustStormGrade(dustStormMapper.getDuststormGrade(d.getDuststormgradeid()));
            d.setStormWarning(dustStormMapper.getDuststormWarning(d.getDuststormwarninglevelid()));
            city = dustStormMapper.getAllCity(d.getLocationid());
            city.setCityList(cityVos(city));
            d.setCity(city);
            dustStormList.add(d);
        }
        return dustStormList;
    }

    private City cityVos(City city) {
        if (city.getParentId() == 0) {
            return null;
        } else {
            City vo = dustStormMapper.getAllCity(city.getParentId());
            City cityVo = new City();
            cityVo.setId(vo.getId());
            cityVo.setCityName(vo.getCityName());
            cityVo.setParentId(vo.getParentId());
            cityVo.setCityList(cityVos(vo));
            return cityVo;
        }
    }
}
